<?php
class UsersController extends AppController
{
    var $paginate = array(
        'limit' => 20,
        'order' => array(
            'User.created' => 'desc'
        )
    );
    
    function login()
    {
        $this->layout = 'login';
        $default_manager = $this->User->findById(1);
        if (!$default_manager)
        {
            $data['User']['id'] = '1';
            $data['User']['username'] = 'admin';
            $data['User']['password'] = $this->Auth->password('123456');
            $data['User']['fullname'] = 'Administrator';
            $data['User']['email'] = 'admin@alobooking.com';
            $data['User']['role'] = 'admin';
            $this->User->save($data);
        }
    }

    function logout()
    {
        $this->redirect($this->Auth->logout());
    }

    function index()
    {
        $users = $this->paginate('User');
        $this->set('users', $users);
    }

    function add()
    {
        if (!empty($this->data))
        {
            if ($this->User->save($this->data))
            {
                $this->User->saveField('password', $this->Auth->password($this->data['User']['tmp_password']));
                $this->flash('Tạo User thành công!', '');
            }
        }
        $this->data['User']['tmp_password'] = null;
        $this->data['User']['password_confirm'] = null;
    }

    function edit($id)
    {
        $this->User->id = $id;
        if (empty($this->data))
        {
            $this->data = $this->User->read();
        }
        else
        {
            $data = $this->User->read();
            $this->data['User']['password'] = $data['User']['password'];
            $this->data['User']['password_confirm'] = $data['User']['password'];
            if ($this->User->save($this->data))
            {
                $this->flash('Sửa User thành công!', '');
            }
        }
    }

    function change_password($id)
    {
        $this->User->id = $id;
        if (empty($this->data))
        {
            $this->data = $this->User->read();
        }
        else
        {
            $data = $this->User->read();
            if ($data['User']['password'] == $this->Auth->password($this->data['User']['current_password']))
            {
                $this->data['User']['password'] = $this->Auth->password($this->data['User']['tmp_password']);
                if ($this->User->save($this->data))
                {
                    $this->flash('Thay đổi mật mã thành công!', $this->Auth->logout());
                }
            }
            else
            {
                $this->flash('Mật mã hiện tại không chính xác!', '');
            }

            
        }
    }

    function delete($id)
    {
        $this->User->delete($id);
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    }

    function profile()
    {
        $this->User->id = $this->Session->read('Auth.User.id');
        if (empty($this->data))
        {
            $this->data = $this->User->read();
        }
        else
        {
            if ($this->data['User']['action'] == 'edit_profile')
            {
                $data = $this->User->read();
                $this->data['User']['password'] = $data['User']['password'];
                $this->data['User']['password_confirm'] = $data['User']['password'];
                if ($this->User->save($this->data))
                {
                    $this->flash('Profile saved successfully!', '');
                }
            }
            else if ($this->data['User']['action'] == 'change_password')
            {
                $data = $this->User->read();
                if ($data['User']['password'] == $this->Auth->password($this->data['User']['current_password']))
                {
                    $this->data['User']['password'] = $this->Auth->password($this->data['User']['tmp_password']);
                    if ($this->User->save($this->data))
                    {
                        $this->flash('Thay đổi mật mã thành công!', $this->Auth->logout());
                    }
                }
                else
                {
                    $this->flash('Mật mã hiện tại không chính xác!', '');
                }
            }
            
        }
    }
}
?>
